package com.demostic.demostic_test.ui;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.PayloadCallback;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACException;
import com.demostic.demostic_test.R;
import com.demostic.demostic_test.utils.Constants;
import com.demostic.demostic_test.utils.MyLog;
import com.demostic.demostic_test.utils.ToastUtils;

import java.util.Timer;
import java.util.TimerTask;

public class PopupWindowWithMask extends PopupWindow {
    private final String TAG = PopupWindowWithMask.class.getSimpleName();
    private final int WHAT_PAUSE = 1;
    private ACDeviceMsg acDeviceMsg;
    protected Context context;
    private int curStatus;
    Handler handler = new Handler() {
        /* class com.demostic.demostic_test.ui.PopupWindowWithMask.AnonymousClass1 */

        @Override // android.os.Handler
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    PopupWindowWithMask.this.acDeviceMsg.setContent(new byte[]{5});
                    PopupWindowWithMask.this.sendToDeviceWithOption(PopupWindowWithMask.this.acDeviceMsg, PopupWindowWithMask.this.physicalDeviceId);
                    return;
                default:
                    return;
            }
        }
    };
    private ImageView image_down;
    private ImageView image_left;
    private ImageView image_right;
    private ImageView image_up;
    private long lastDownTime;
    private String physicalDeviceId;
    private String subdomain;
    private Task timeTask;
    private Timer timer;
    private ViewGroup vg;

    public PopupWindowWithMask(Context context2, ViewGroup vg2, String physicalDeviceId2, String subdomain2) {
        super(context2);
        this.context = context2;
        this.vg = vg2;
        this.physicalDeviceId = physicalDeviceId2;
        this.subdomain = subdomain2;
        setContentView(initContentView());
        setHeight(initHeight());
        setWidth(initWidth());
        setOutsideTouchable(true);
        setTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable());
    }

    @Override // android.widget.PopupWindow
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        this.acDeviceMsg = new ACDeviceMsg();
        this.acDeviceMsg.setCode(73);
        this.timer = new Timer();
        this.timeTask = new Task();
        this.timer.schedule(this.timeTask, 0, 4000);
    }

    public View initContentView() {
        View view = LayoutInflater.from(this.context).inflate(R.layout.window_direction, (ViewGroup) null);
        this.image_left = (ImageView) view.findViewById(R.id.image_left);
        this.image_right = (ImageView) view.findViewById(R.id.image_right);
        this.image_up = (ImageView) view.findViewById(R.id.image_up);
        this.image_down = (ImageView) view.findViewById(R.id.image_down);
        if (this.subdomain.equals(Constants.subdomain_x800) || this.subdomain.equals("ilifex660") || this.subdomain.equals("ilifex900")) {
            this.image_down.setImageResource(R.drawable.map_aty_dir_down);
        } else {
            this.image_down.setImageResource(R.drawable.map_aty_down_bg);
        }
        setListener();
        return view;
    }

    public void setListener() {
        this.image_left.setOnTouchListener(new MyTouchListener());
        this.image_right.setOnTouchListener(new MyTouchListener());
        this.image_up.setOnTouchListener(new MyTouchListener());
        if (!this.subdomain.equals(Constants.subdomain_x800) && !this.subdomain.equals("ilifex660") && !this.subdomain.equals("ilifex900")) {
            this.image_down.setOnTouchListener(new MyTouchListener());
        }
    }

    public int initHeight() {
        return -2;
    }

    public int initWidth() {
        return -2;
    }

    public void setAllUnEnable() {
        this.image_left.setEnabled(false);
        this.image_right.setEnabled(false);
        this.image_up.setEnabled(false);
        this.image_down.setEnabled(false);
    }

    public void setAllEnable() {
        this.image_left.setEnabled(true);
        this.image_right.setEnabled(true);
        this.image_up.setEnabled(true);
        this.image_down.setEnabled(true);
    }

    public void sendToDeviceWithOption(ACDeviceMsg deviceMsg, String physicalDeviceId2) {
        AC.bindMgr().sendToDeviceWithOption(this.subdomain, physicalDeviceId2, deviceMsg, 2, new PayloadCallback<ACDeviceMsg>() {
            /* class com.demostic.demostic_test.ui.PopupWindowWithMask.AnonymousClass2 */

            public void success(ACDeviceMsg deviceMsg) {
                switch (deviceMsg.getCode()) {
                    case 73:
                        MyLog.e(PopupWindowWithMask.this.TAG, "Action sendToDeviceWithOption==:" + ((int) deviceMsg.getContent()[0]));
                        return;
                    default:
                        return;
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                ToastUtils.showErrorToast(PopupWindowWithMask.this.context, e.getErrorCode());
            }
        });
    }

    /* access modifiers changed from: package-private */
    public class MyTouchListener implements View.OnTouchListener {
        MyTouchListener() {
        }

        @Override // android.view.View.OnTouchListener
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == 0) {
                PopupWindowWithMask.this.setAllUnEnable();
                v.setEnabled(true);
                v.setSelected(true);
                MyLog.e(PopupWindowWithMask.this.TAG, "onTouch  MotionEvent.ACTION_DOWN System.currentTimeMillis()-lastDownTime<400  " + (System.currentTimeMillis() - PopupWindowWithMask.this.lastDownTime < 400));
                if (System.currentTimeMillis() - PopupWindowWithMask.this.lastDownTime < 400) {
                    v.setSelected(false);
                    PopupWindowWithMask.this.setAllEnable();
                    return false;
                }
                PopupWindowWithMask.this.lastDownTime = System.currentTimeMillis();
                PopupWindowWithMask.this.timeTask.setId(v.getId());
                PopupWindowWithMask.this.timeTask.run();
            } else if (event.getAction() == 1) {
                PopupWindowWithMask.this.setAllEnable();
                v.setSelected(false);
                PopupWindowWithMask.this.timeTask.setId(0);
                PopupWindowWithMask.this.timeTask.run();
                if (PopupWindowWithMask.this.subdomain.equals(Constants.subdomain_x782) || PopupWindowWithMask.this.subdomain.equals(Constants.subdomain_x785) || PopupWindowWithMask.this.subdomain.equals(Constants.subdomain_x786) || PopupWindowWithMask.this.subdomain.equals(Constants.subdomain_x787) || PopupWindowWithMask.this.subdomain.equals(Constants.subdomain_x790) || PopupWindowWithMask.this.subdomain.equals(Constants.subdomain_x800) || PopupWindowWithMask.this.subdomain.equals("ilifex900") || PopupWindowWithMask.this.subdomain.equals("ilifex900")) {
                    PopupWindowWithMask.this.handler.sendEmptyMessageDelayed(1, 300);
                }
                MyLog.e(PopupWindowWithMask.this.TAG, "Action onTouch UP==:");
            }
            return true;
        }
    }

    /* access modifiers changed from: package-private */
    public class Task extends TimerTask {
        int resId = 0;

        public Task() {
        }

        public void setId(int id) {
            this.resId = id;
        }

        @Override // java.util.TimerTask, java.lang.Runnable
        public void run() {
            MyLog.e(PopupWindowWithMask.this.TAG, "running==:");
            if (this.resId != 0) {
                switch (this.resId) {
                    case R.id.image_down /* 2131755632 */:
                        PopupWindowWithMask.this.acDeviceMsg.setContent(new byte[]{2});
                        break;
                    case R.id.image_up /* 2131755743 */:
                        PopupWindowWithMask.this.acDeviceMsg.setContent(new byte[]{1});
                        break;
                    case R.id.image_left /* 2131755744 */:
                        PopupWindowWithMask.this.acDeviceMsg.setContent(new byte[]{3});
                        break;
                    case R.id.image_right /* 2131755745 */:
                        PopupWindowWithMask.this.acDeviceMsg.setContent(new byte[]{4});
                        break;
                }
                PopupWindowWithMask.this.sendToDeviceWithOption(PopupWindowWithMask.this.acDeviceMsg, PopupWindowWithMask.this.physicalDeviceId);
                MyLog.e(PopupWindowWithMask.this.TAG, "Action Task==:" + ((int) PopupWindowWithMask.this.acDeviceMsg.getContent()[0]));
            }
        }
    }

    @Override // android.widget.PopupWindow
    public void dismiss() {
        super.dismiss();
        MyLog.e(this.TAG, "Action dismiss==:" + this.curStatus);
        if (this.curStatus == 10) {
            sendToDeviceWithOption(new ACDeviceMsg(70, new byte[]{2}), this.physicalDeviceId);
        }
        if (this.timer != null) {
            this.timer.cancel();
        }
    }

    public void setCurStatus(int curStatus2) {
        this.curStatus = curStatus2;
    }
}
